package com.id.sigma.camel;

import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CamelService {
	private static CamelService instance;
	
	ApplicationContext context;
	ProducerTemplate camelTemplate;
//	private String messege = null;
	
	public String getMessege() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"CamelBeans.xml");
		ConsumerTemplate cTemplate = context.getBean("camelConsumer",
				ConsumerTemplate.class);
		Exchange ex = cTemplate.receive("jms:queue:Misbah");
		String message = (String) ex.getIn().getBody();
		
		return message;
	}

//	public void setMessege(String messege) {
//		this.messege = messege;
//	}

	public static CamelService getInstance(){
		if (instance==null) 
			instance = new CamelService();
		return instance;
	}
	
	public CamelService(){
		context = new ClassPathXmlApplicationContext("CamelBeans.xml");
		camelTemplate = context.getBean("camelTemplate",ProducerTemplate.class);
	}
	
	public void sendToActiveMq(String message, String from){
		System.out.println("Messege = "+message);
		camelTemplate = context.getBean("camelTemplate",ProducerTemplate.class);
		camelTemplate.sendBody(from, message);
	}
	
}
